/******************************************************************************
*  COPYRIGHT MorningCore CO.,LTD
******************************************************************************/

/*****************************************************************
* FileName::    <SRP_TOP.c>
* version::     <1.0.0>
* Purpose:      <the uplink SRP top function>
* Authors:      <shanshan, 2022-11-14>
* Notes:        <no>
******************************************************************/

/******************************************************************************
*  HISTORY OF CHANGES
*   <Date>          <Author>        <Version>       <DESCRIPTION>
*   2022-11-14      shanshan        V0.1.0          original
******************************************************************************/

/******************************************************************************
*  INCLUDE FILES

******************************************************************************/
#include "ue_tx_prach_srp_top_fix.h"
#include "../common/head/NR_UL_macro.h"
/*----------- function definition-----------------------------------*/
/*****************************************************************************
* Function: SRP_TOP()
* Purpose:  This function performs symbol processing top
*
* Relation:
Calls       : ....
Called by   : ....
* Params:
*   <Name>          <Type>      <In/Out>    <Description>

  * Return:  void
  * Note:    <no>
*******************************************************************************/
void PRACH_SRP_Top_fix
(
    /********************************* Interface *****************************/
    //Output
    cmplx_frN  *SRP_PRACH_Op,
    int        BitLength,
    /********************************* Parameter *****************************/
    stULParamTable *SimParam
)
{
    /*variables definition*/
    cmplx_frN  ZCRootSeqGen_Op[839];     // ZC Root Sequence Output
    cmplx_frN  SRP_PRACH_FE_Op[4096];     // PRACH front end output
    cmplx_frN  *SRP_PRACH_IFFT_Op; //[2048*2+1752];
    cmplx_frN  *SRP_PRACH_Filter_Op;

    int   n;//for cycle
	char   cFileAddr[256] = { 0 };

    //////// Zadoff-Chu sequence generation ////////////////
    for (n = 0; n < 4096; n++)
    {
        SRP_PRACH_FE_Op[n].re = 0;
        SRP_PRACH_FE_Op[n].im = 0;
    }

    SRP_PRACH_IFFT_Op    = (cmplx_frN *) malloc(sizeof(cmplx_frN) * SimParam->RA_IFFT_Op_Len);

    for (n = 0; n < SimParam->RA_IFFT_Op_Len; n++)
    {
        SRP_PRACH_IFFT_Op[n].re = 0;
        SRP_PRACH_IFFT_Op[n].im = 0;
    }

    SRP_PRACH_Filter_Op = (cmplx_frN *) malloc(sizeof(cmplx_frN) * SimParam->RA_Filter_Op_Len);
    for (n = 0; n < SimParam->RA_Filter_Op_Len; n++)
    {
        SRP_PRACH_Filter_Op[n].re = 0;
        SRP_PRACH_Filter_Op[n].im = 0;
    }

    ZCRootSeqGen_fix
    (
        SimParam->L_RA,   // 839 or 139 only for PRACH
        SimParam->PRACH_ZC_u, // u (PRACH) or q (RS) in 3GPP TS 38.211
        BitLength,
        ZCRootSeqGen_Op // Zadoff-Chu root sequence
    );

#if UE_TX_PRINTFLAG
	sprintf_s(cFileAddr, "Result/PRACHTestData/%d/ZCRootSeqGen_Out.dat", CASEIDX_PRINT);
	vFilePrintDataFixpQI(cFileAddr, &ZCRootSeqGen_Op[0], SimParam->L_RA, BitLength);
#else
	sprintf_s(cFileAddr, "Result/PRACHTestData/%d/ZCRootSeqGen_Out.dat", CASEIDX_PRINT);
	vFilePrintData_Fxp2Flt(cFileAddr, &ZCRootSeqGen_Op[0], SimParam->L_RA, 8192);
#endif

    ///////////// SRP FE start /////////////////////
    SRP_PRACH_FE_fix
    (
        ZCRootSeqGen_Op,    // Root seq from ZCRootSeqGen
        SimParam,
        BitLength,// processing bit width
        SRP_PRACH_FE_Op     // output of SRP_PRACH_FE
    );
    /////////////// SRP FE end ///////////////////

    ////////// SRP BE start /////////////////
    SRP_PRACH_BE_fix
    (
		SRP_PRACH_FE_Op,
        SimParam,           // param
        BitLength,// bit width
        SRP_PRACH_IFFT_Op,  // PRACH IFFT Buffer
        SRP_PRACH_Filter_Op,// UpSampling filter Buffer
        SRP_PRACH_Op        // PRACH output buffer
    );
	////////// SRP BE end /////////////////

    free(SRP_PRACH_IFFT_Op);
    free(SRP_PRACH_Filter_Op);
}

